<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="维护编号" prop="maintenanceCode">
        <el-input
          v-model="queryParams.maintenanceCode"
          placeholder="请输入维护编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备ID" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维护类型" prop="maintenanceType">
        <el-select v-model="queryParams.maintenanceType" placeholder="请选择维护类型" clearable>
          <el-option label="预防性维护" value="预防性维护" />
          <el-option label="纠正性维护" value="纠正性维护" />
          <el-option label="紧急维护" value="紧急维护" />
          <el-option label="计划维护" value="计划维护" />
        </el-select>
      </el-form-item>
      <el-form-item label="维护状态" prop="maintenanceStatus">
        <el-select v-model="queryParams.maintenanceStatus" placeholder="请选择维护状态" clearable>
          <el-option label="待维护" value="待维护" />
          <el-option label="维护中" value="维护中" />
          <el-option label="已完成" value="已完成" />
          <el-option label="已取消" value="已取消" />
        </el-select>
      </el-form-item>
      <el-form-item label="维护日期" prop="maintenanceDate">
        <el-date-picker
          v-model="queryParams.maintenanceDate"
          type="date"
          placeholder="选择维护日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['equipment:maintenance:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['equipment:maintenance:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['equipment:maintenance:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['equipment:maintenance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维护ID" align="center" prop="maintenanceId" />
      <el-table-column label="维护编号" align="center" prop="maintenanceCode" />
      <el-table-column label="设备ID" align="center" prop="equipmentId" />
      <el-table-column label="维护类型" align="center" prop="maintenanceType" />
      <el-table-column label="维护日期" align="center" prop="maintenanceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.maintenanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维护状态" align="center" prop="maintenanceStatus">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.maintenanceStatus === '已完成' ? 'success' : 
                   scope.row.maintenanceStatus === '维护中' ? 'warning' : 
                   scope.row.maintenanceStatus === '已取消' ? 'danger' : 'info'"
          >
            {{ scope.row.maintenanceStatus }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="维护结果" align="center" prop="maintenanceResult" />
      <el-table-column label="维护费用" align="center" prop="maintenanceCost">
        <template slot-scope="scope">
          <span>{{ scope.row.maintenanceCost ? '￥' + scope.row.maintenanceCost : '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行人" align="center" prop="executor" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['equipment:maintenance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['equipment:maintenance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备维护记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="维护编号" prop="maintenanceCode">
              <el-input v-model="form.maintenanceCode" placeholder="请输入维护编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备ID" prop="equipmentId">
              <el-input v-model="form.equipmentId" placeholder="请输入设备ID" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="维护类型" prop="maintenanceType">
              <el-select v-model="form.maintenanceType" placeholder="请选择维护类型">
                <el-option label="预防性维护" value="预防性维护" />
                <el-option label="纠正性维护" value="纠正性维护" />
                <el-option label="紧急维护" value="紧急维护" />
                <el-option label="计划维护" value="计划维护" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维护状态" prop="maintenanceStatus">
              <el-select v-model="form.maintenanceStatus" placeholder="请选择维护状态">
                <el-option label="待维护" value="待维护" />
                <el-option label="维护中" value="维护中" />
                <el-option label="已完成" value="已完成" />
                <el-option label="已取消" value="已取消" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="维护日期" prop="maintenanceDate">
              <el-date-picker
                v-model="form.maintenanceDate"
                type="date"
                placeholder="选择维护日期"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维护费用" prop="maintenanceCost">
              <el-input-number v-model="form.maintenanceCost" :precision="2" :min="0" controls-position="right" style="width: 100%" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="执行人" prop="executor">
          <el-input v-model="form.executor" placeholder="请输入执行人" />
        </el-form-item>
        <el-form-item label="维护内容" prop="maintenanceContent">
          <el-input v-model="form.maintenanceContent" type="textarea" placeholder="请输入维护内容" :rows="3" />
        </el-form-item>
        <el-form-item label="维护结果" prop="maintenanceResult">
          <el-input v-model="form.maintenanceResult" type="textarea" placeholder="请输入维护结果" :rows="3" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :rows="2" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { 
  listMaintenance, 
  getMaintenance, 
  delMaintenance, 
  addMaintenance, 
  updateMaintenance,
  checkMaintenanceCodeUnique
} from "@/api/mes/equipment/maintenance"

export default {
  name: "Maintenance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备维护记录表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maintenanceCode: undefined,
        equipmentId: undefined,
        maintenanceType: undefined,
        maintenanceStatus: undefined,
        maintenanceDate: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        maintenanceCode: [
          { required: true, message: "维护编号不能为空", trigger: "blur" },
          { validator: this.validateMaintenanceCode, trigger: "blur" }
        ],
        equipmentId: [
          { required: true, message: "设备ID不能为空", trigger: "blur" }
        ],
        maintenanceType: [
          { required: true, message: "维护类型不能为空", trigger: "change" }
        ],
        maintenanceStatus: [
          { required: true, message: "维护状态不能为空", trigger: "change" }
        ],
        maintenanceDate: [
          { required: true, message: "维护日期不能为空", trigger: "change" }
        ],
        executor: [
          { required: true, message: "执行人不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询设备维护记录列表 */
    getList() {
      this.loading = true
      listMaintenance(this.queryParams).then(response => {
        this.maintenanceList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        maintenanceId: undefined,
        maintenanceCode: undefined,
        equipmentId: undefined,
        maintenanceType: undefined,
        maintenanceDate: undefined,
        maintenanceStatus: "待维护",
        maintenanceContent: undefined,
        maintenanceResult: undefined,
        maintenanceCost: undefined,
        executor: undefined,
        remark: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.maintenanceId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加设备维护记录"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const maintenanceId = row.maintenanceId || this.ids
      getMaintenance(maintenanceId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改设备维护记录"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.maintenanceId != undefined) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const maintenanceIds = row.maintenanceId || this.ids
      this.$modal.confirm('是否确认删除维护编号为"' + maintenanceIds + '"的数据项？').then(function() {
        return delMaintenance(maintenanceIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('equipment/maintenance/export', {
        ...this.queryParams
      }, `maintenance_${new Date().getTime()}.xlsx`)
    },
    /** 维护编号唯一性校验 */
    validateMaintenanceCode(rule, value, callback) {
      if (value) {
        checkMaintenanceCodeUnique(value).then(response => {
          if (response.data) {
            callback()
          } else {
            callback(new Error("维护编号已存在"))
          }
        })
      } else {
        callback()
      }
    }
  }
}
</script>